//
//  NewEventRemindersSelectedCell.swift
//  YOKO
//
//  Created by Zoe_G on 16/5/31.
//  Copyright © 2016年 15Tec. All rights reserved.
//

import UIKit

protocol ReminderCellDelegate {
    func reminderDidSelected(reminder: String)
}

class NewEventRemindersSelectedCell: UITableViewCell , UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var upShadowView: UIView!
    @IBOutlet weak var downShadowView: UIView!
    
    var delegate: ReminderCellDelegate! = nil
    
    var tableView: UITableView!
    var tableViewData = [String](){
        didSet{
            if !tableViewData.isEmpty {
                for _ in 1...self.tableViewData.count {
                    let selectedBtn = UIButton(frame: CGRectMake(screenWidth-125.0, 5.0, 24.0, 24.0))
                    selectedBtn.setImage(UIImage(named: "圈圈"), forState: .Normal)
                    selectedBtn.setImage(UIImage(named: "time_choose"), forState: .Selected)
                    btns.append(selectedBtn)
                }
                self.tableView.reloadData()
            }
        }
    }
    var selectedReminders = 0 {
        didSet {
            self.delegate.reminderDidSelected(String(selectedReminders))
        }
    }
    
    var btns = [UIButton]()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        upShadowView.layer.shadowOffset = CGSizeMake(0, -3)
        upShadowView.layer.shadowColor = UIColor.lightGrayColor().CGColor
        upShadowView.layer.shadowRadius = 3
        upShadowView.layer.shadowOpacity = 0.8
        
        downShadowView.layer.shadowOffset = CGSizeMake(0, 3)
        downShadowView.layer.shadowColor = UIColor.lightGrayColor().CGColor
        downShadowView.layer.shadowRadius = 3
        downShadowView.layer.shadowOpacity = 0.8
        
        self.backgroundColor = YOKOColor.backImageColor
        
        tableView = UITableView(frame: CGRectMake(48.0, 3, screenWidth - 96.0, self.bounds.height - 3))
        tableView.scrollEnabled = false
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.clearColor()
        tableView.separatorStyle = .None
        
        self.contentView.addSubview(tableView)
        
    }
    
    //MARK:- UITableViewDelegate
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableViewData.count
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = UITableViewCell()
        cell.backgroundColor = UIColor.clearColor()
        cell.textLabel?.textColor = YOKOColor.fontBlack
        let font = UIFont.init(name: "HelveticaNeue", size: 14)
        let textAttrs = [NSFontAttributeName: font!, NSKernAttributeName:0.5]
        cell.textLabel?.attributedText = NSAttributedString(string: tableViewData[indexPath.row], attributes: textAttrs)

        
        if ((1 << indexPath.row) & selectedReminders) != 0 {
            btns[indexPath.row].selected = true
        }
        
        cell.addSubview(btns[indexPath.row])
        
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        btns[indexPath.row].selected = !btns[indexPath.row].selected
        
        let currentSelection = 1 << indexPath.row
        selectedReminders = selectedReminders ^ currentSelection
        
        self.delegate.reminderDidSelected(String(selectedReminders))
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 35.0
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
